#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef double db;
inline LL read()
{
	LL x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
LL l,r,num[20];
LL f[20][1<<8][2530];
LL dfs(LL dep,LL stat,LL sum,bool flag)
{
	if(!dep)
	{
		for(LL i=0;i<=7;++i)
			if(stat&(1<<i)&&(sum%(i+2)))return 0;
		return 1;
	}
	if(!flag&&~f[dep][stat][sum])return f[dep][stat][sum];
	LL _max=flag?num[dep]:9,ret=0;
	for(LL i=0;i<=_max;++i)
		ret+=dfs(dep-1,i<2?stat:(stat|(1<<(i-2))),(sum*10+i)%2520,flag&(i==num[dep]));
	if(!flag)f[dep][stat][sum]=ret;
	return ret;
}
LL cal(LL x)
{
	LL ret,tot=0;
	while(x)
	{
		num[++tot]=x%10;
		x/=10;
	}
	ret=dfs(tot,0,0,true);
	return ret;
}
int main()
{
	LL T=read();
	memset(f,-1,sizeof(f));
	while(T--)
	{
		l=read();r=read();
		printf("%I64d\n",cal(r)-cal(l-1));
	}
	return 0;
}
